package com.ltf.config;

import com.ltf.utils.PackJson;
import org.springframework.security.access.AccessDeniedException;
import org.springframework.security.web.access.AccessDeniedHandler;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

public class JwtAccessDeniedHandler  implements AccessDeniedHandler {
    @Override
    public void handle(HttpServletRequest request,
                       HttpServletResponse response,
                       AccessDeniedException accessDeniedException) throws IOException, ServletException {
        //403表示权限不足  401用户登陆失败（身份验证失败，即authenticaiton失败）
        response.setStatus(403);
        response.setCharacterEncoding("UTF-8");
        response.setContentType("application/json");
        response.getWriter().println(PackJson.packJson(403,"权限不足，无法访问",accessDeniedException.getMessage()));
        response.flushBuffer();//刷新

    }
}
